# syntax = docker/dockerfile:experimental
ARG CDN_REGION=cn
ARG APP_ENV=dev
ARG DOT_ENV_POSTFIX=dev
ARG PROJECT_MOD_BASENAME=port
ARG PRJ_BASENAME=bizark_port_web
ARG DOMAIN=thebizark.com
ARG DEBUGMODE=debug

FROM node:12 AS builder
LABEL maintainer="Nick Fan <nickfan81@gmail.com>"
ARG APP_ENV
ENV APP_ENV=${APP_ENV}
ARG DOT_ENV_POSTFIX
ENV DOT_ENV_POSTFIX=${DOT_ENV_POSTFIX}
ARG CDN_REGION
ENV CDN_REGION=${CDN_REGION}
ARG PRJ_BASENAME
ENV PRJ_BASENAME=${PRJ_BASENAME}
ARG PROJECT_MOD_BASENAME
ENV PROJECT_MOD_BASENAME=${PROJECT_MOD_BASENAME}
ARG DOMAIN
ENV DOMAIN=${DOMAIN}

RUN addgroup www && adduser  --ingroup www --disabled-password www
USER www
ENV HOME /home/www
ENV HOMEPATH /home/www
WORKDIR ${HOMEPATH}/Code/${PRJ_BASENAME}/current
COPY .npmrc_cn .npmrc_cn
COPY .yarnrc_cn .yarnrc_cn
USER root
RUN if [ "${CDN_REGION}" = "cn" ]; then \
    cp /etc/apt/sources.list /etc/apt/sources.list.backup ;\
    sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list ;\
    cp -rf .npmrc_cn /root/.npmrc ;\
    cp -rf .yarnrc_cn /root/.yarnrc ;\
    npm install -g nrm yrm cnpm cyarn --registry=https://registry.npm.taobao.org ;\
    nrm use taobao ;\
    yrm use taobao ;\
    else \
    touch /root/.npmrc ;\
    touch /root/.yarnrc ;\
    fi
#RUN if [ "${CDN_REGION}" = "cn" ]; then \
#        npm install -g nrm yrm cnpm cyarn typescript npm-check @vue/cli @vue/cli-service-global @vue/cli-init --registry=https://registry.npm.taobao.org ;\
#    else \
#        npm install -g nrm yrm cnpm cyarn typescript npm-check @vue/cli @vue/cli-service-global @vue/cli-init ;\
#    fi
RUN npm cache verify && npm install -g nrm yrm cnpm cyarn typescript npm-check @vue/cli @vue/cli-service-global @vue/cli-init
RUN if [ "${CDN_REGION}" = "cn" ]; then \
    cp -af .npmrc_cn ${HOMEPATH}/.npmrc ;\
    cp -af .yarnrc_cn ${HOMEPATH}/.yarnrc ;\
    nrm use taobao ;\
    yrm use taobao ;\
    else \
    touch ${HOMEPATH}/.npmrc ;\
    touch ${HOMEPATH}/.yarnrc ;\
    fi
RUN chown -R www:www ${HOMEPATH}/.npmrc && chown -R www:www ${HOMEPATH}/.yarnrc
COPY package.json package.json
COPY bizark_fe_mods/package.json ./bizark_fe_mods/package.json
COPY .env.${DOT_ENV_POSTFIX} .env
RUN mkdir -p ${HOMEPATH}/Code/${PRJ_BASENAME}/current/node_modules && chown -R www:www ${HOMEPATH}/Code/${PRJ_BASENAME}/current && chown -R www:www ${HOMEPATH}/Code/${PRJ_BASENAME}/current/node_modules
USER www
RUN if [ "${CDN_REGION}" = "cn" ]; then \
    nrm use taobao ; \
    yrm use taobao ; \
    /usr/local/bin/cnpm i ;\
    else \
    /usr/local/bin/npm i ;\
    fi
USER root
RUN mkdir -p /etc/ssl/nginx/certs && \
    chown -R www:www /etc/ssl/nginx/certs && \
    ln -nfs ${HOMEPATH}/Code /home/wwwroot && \
    ln -nfs /etc/ssl/nginx/certs ${HOMEPATH}/Code/certs
USER www
COPY . .
USER root
RUN mkdir -p dist && mkdir -p logs && \
    find ${HOMEPATH}/Code/${PRJ_BASENAME}/current -mindepth 1 -maxdepth 1 \( -path ${HOMEPATH}/Code/${PRJ_BASENAME}/current/node_modules \) -prune -o -print0 |xargs -0 chown -R www:www && \
    chmod +x ${HOMEPATH}/Code/${PRJ_BASENAME}/current/docker-entrypoint.sh
USER www
#RUN git submodule update --init --recursive && git -C ${HOMEPATH}/Code/${PRJ_BASENAME}/current/bizark_fe_mods checkout dev && git -C ${HOMEPATH}/Code/${PRJ_BASENAME}/current/bizark_fe_mods pull
#RUN if [ "${APP_ENV}" = "prod" ]; then \
#    npm run build ;\
#    else \
#    npm run build:${APP_ENV} ;\
#    fi
CMD ["${APP_ENV}","${DEBUGMODE}"]
ENTRYPOINT ["./docker-entrypoint.sh"]
VOLUME ["/home/www/Code/bizark_port_web/current/dist","/home/www/Code/bizark_port_web/current/src","/home/www/Code/bizark_port_web/current/public","/home/www/Code/bizark_port_web/current/bizark_fe_mods","/home/wwwlogs","/etc/ssl/nginx/certs"]
EXPOSE 80 443 8443 8080
#ENTRYPOINT [ "tail", "-f", "/dev/null" ]
